﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class LevelNumberLetter : MonoBehaviour
{
    public List<TextButton> numberList = new List<TextButton>();
    private List<int> letterList = new List<int>();
    private int clickCount = 0;
    private string tempTargetStr = string.Empty;
    public List<Transform> lovePosList = new List<Transform>();
    public List<int> loveNumberIndexList = new List<int>();


    public GameLevelItem gameLevelItem;
    void Start()
    {
        InitLetterList();
        InitNumberBtn();
        loveNumberIndexList.Clear();
        loveNumberIndexList.Add(14);
        loveNumberIndexList.Add(3);
        loveNumberIndexList.Add(10);
        loveNumberIndexList.Add(13);
        loveNumberIndexList.Add(4);
    }

    private void InitLetterList()
    {
        letterList.Add(1);
        letterList.Add(22);
        letterList.Add(3);
        letterList.Add(9);
        letterList.Add(20);
        letterList.Add(15);
        letterList.Add(13);
        letterList.Add(24);
        letterList.Add(11);
        letterList.Add(25);
        letterList.Add(7);
        letterList.Add(21);
        letterList.Add(23);
        letterList.Add(8);
        letterList.Add(12);
        letterList.Add(26);
    }

    private void InitNumberBtn()
    {
        int length = numberList.Count;
        if (letterList.Count != length)
            Debug.LogError("数字的数量和按钮数量不匹配");
        for (int i = 0; i < length; i++)
        {
            int index = i;
            numberList[index].Init(letterList[index].ToString(), () =>
            {
                OnNumberButtonClick(index);
            });
        }
    }

    private void OnNumberButtonClick(int index)
    {
        clickCount++;
        tempTargetStr += GetNumberStr(letterList[index]);
        //tweenList[index].Play(PlayDirection.Forward);
        numberList[index].text.DOFade(0, 0.3f);
        numberList[index].button.enabled = false;
        if (clickCount == 5)
        {
            if (tempTargetStr.Equals(UConst.Str_TargetLove))
            {
                WinAni();
            }
            else
            {
                gameLevelItem.TriggerResult(false);
            }
        }
    }

    private void WinAni()
    {
        int count = numberList.Count;
        for (int i = 0; i < count; i++)
        {
            if (UConst.Str_TargetLove.Contains(GetNumberStr(letterList[i])))
            {
                numberList[i].text.text = GetNumberStr(letterList[i]);
                numberList[i].text.DOFade(1, 1f);
            }
            else
            {
                numberList[i].text.DOFade(0, 1f);
            }
        }

        Invoke("WinMoveAni", 1.3f);
    }

    private void WinMoveAni()
    {
        int count = loveNumberIndexList.Count;
        for (int i = 0; i < count; i++)
        {
            numberList[loveNumberIndexList[i]].transform.DOLocalMove(lovePosList[i].localPosition, 0.8f);
        }
        Invoke("TriggerWin", 1.3f);
    }

    private void TriggerWin()
    {
        gameLevelItem.TriggerResult(true);
    }

    private string GetNumberStr(int index)
    {
        string letterStr = string.Empty;    
        switch (index)
        {
            case 12:
                letterStr = "l";
                break;
            case 9:
                letterStr = "i";
                break;
            case 7:
                letterStr = "g";
                break;
            case 8:
                letterStr = "h";
                break;
            case 20:
                letterStr = "t";
                break;
            default:
                letterStr = "a";
                break;
        }
        return letterStr;

    }



}
